package cn.xuchunh.sword;

import java.util.HashMap;

/**
 * 在一个字符串(1<=字符串长度<=10000，全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置
 * Created by XuChunH on 2016/9/18.
 */
public class FirstNotRepeatingChar {

    public int solution(String source){
        if(source == null || source.isEmpty()){
            return -1;
        }
        int index = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int size = source.length();
        for (int i = 0; i < size; i++) {
            char temp = source.charAt(i);
            if(map.containsKey(temp)){
                map.put(temp, map.get(temp) + 1);
            }else {
                map.put(temp, 1);
            }
        }
        for (int i = 0; i < size; i++) {
            if(map.get(source.charAt(i)) == 1){
                index = i;
                break;
            }
        }
        return index;
    }

}
